home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / EXISTSX.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  5KB  |  166 lines

  1. /* +++Date last modified: 02-Sep-1996 */
  2.  
  3. /************************************************************************/
  4. /*                                                                      */
  5. /*  EXISTSX.C                                                           */
  6. /*                                                                      */
  7. /*  POSIX-compliant functions to verify the existance of files in the   */
  8. /*  PATH or in other environment variables.                             */
  9. /*                                                                      */
  10. /*  Original Copyright 1990-93 by Robert B. Stout as part of            */
  11. /*  the MicroFirm Function Library (MFL)                                */
  12. /*                                                                      */
  13. /*  The user is granted a free limited license to use this source file  */
  14. /*  to create royalty-free programs, subject to the terms of the        */
  15. /*  license restrictions specified in the LICENSE.MFL file.             */
  16. /*                                                                      */
  17. /************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unistd.h"
  23. #include "snipfile.h"
  24. #include "snip_str.h"
  25.  
  26. /*
  27. **  exists()
  28. **
  29. **  See if a file exists.
  30. **
  31. **  Parameters: 1 - File name
  32. **
  33. **  Returns: True_ if found, else False_.
  34. **
  35. */
  36.  
  37. Boolean_T exists(char *name)
  38. {
  39.       return(Success_ == access(name, F_OK));
  40. }
  41.  
  42. /*
  43. **  dexists()
  44. **
  45. **  See if a file exists in an environment variable.
  46. **
  47. **  Parameters: 1 - File name
  48. **              2 - Environment variable name
  49. **
  50. **  Returns: The pathname of the file which was found or NULL if the file
  51. **           was not found.
  52. **
  53. **  Side effects: The returned path name is a static string. Subesequent
  54. **                calls to any of these [d/p/g]exists() functions will
  55. **                destroy the previous contents.
  56. **
  57. **  Notes: Uses stptok() and dos2unix(), also in SNIPPETS. May require
  58. **         strupr() from SNIPPETS if it's not in your standard library.
  59. */
  60.  
  61. char *dexists(char *name, char *envar)
  62. {
  63.       char *path, *ptr;
  64.       static char newname[FILENAME_MAX];
  65.       int i, j;
  66.       char chr;
  67.  
  68.       if (exists(name))
  69.             return(name);
  70.  
  71.       if (NULL == (path = getenv(strupr(envar))))
  72.             return(NULL);                       /* no environment var.  */
  73.  
  74.       dos2unix(path);
  75.       
  76.       do
  77.       {
  78.             path = stptok(path, newname, FILENAME_MAX, ";");
  79.             if (LAST_CHAR(newname) != '/')
  80.                   strcat(newname, "/");
  81.             strcat(newname, name);
  82.             if (exists(newname))
  83.                   return(newname);
  84.       } while (path && *path);
  85.  
  86.       return NULL;
  87. }
  88.  
  89. /*
  90. **  pexists()
  91. **
  92. **  See if a file exists in the PATH.
  93. **
  94. **  Parameters: 1 - File name
  95. **
  96. **  Returns: The pathname of the file which was found or NULL if the file
  97. **           was not found.
  98. **
  99. **  Side effects: The returned path name is a static string. Subesequent
  100. **                calls to any of these [d/p/g]exists() functions will
  101. **                destroy the previous contents.
  102. */
  103.  
  104. char *pexists(char *name)
  105. {
  106.       return dexists(name, "PATH");
  107. }
  108.  
  109. /*
  110. **  gexists()
  111. **
  112. **  See if a file exists in the PATH or in an environment variable.
  113. **
  114. **  Parameters: 1 - File name
  115. **              2 - Environment variable name
  116. **
  117. **  Returns: The pathname of the file which was found or NULL if the file
  118. **           was not found.
  119. **
  120. **  Side effects: The returned path name is a static string. Subesequent
  121. **                calls to any of these [d/p/g]exists() functions will
  122. **                destroy the previous contents.
  123. */
  124.  
  125. char *gexists(char *name, char *envar)
  126. {
  127.       char *tmp;
  128.  
  129.       if (exists(name))
  130.             return(name);
  131.       if (NULL != (tmp = dexists(name, envar)))
  132.             return(tmp);
  133.       return(pexists(name));
  134. }
  135.  
  136. #ifdef TEST
  137.  
  138. main(int argc, char *argv[])
  139. {
  140.       char *fname;
  141.  
  142.       switch (argc)
  143.       {
  144.       case 2:
  145.             fname = pexists(argv[1]);
  146.             if (fname)
  147.                   printf("%s found in PATH\n", fname);
  148.             else  printf("%s not found in PATH\n", argv[1]);
  149.             break;
  150.       case 3:
  151.             fname = gexists(argv[1], argv[2]);
  152.             if (fname)
  153.                   printf("%s found in PATH or %s\n", fname, argv[2]);
  154.             else  printf("%s not found in PATH or %s\n",
  155.                         argv[1], argv[2]);
  156.             break;
  157.       default:
  158.             puts("\aUsage: EXISTSX fname [envar]");
  159.             return EXIT_FAILURE;
  160.       }
  161.  
  162.       return EXIT_SUCCESS;
  163. }
  164.  
  165. #endif /* TEST */
  166.